package com.hx.hospital.service.impl;

import com.hx.common.constant.RedisKey;
import com.hx.common.exception.CustomerException;
import com.hx.hospital.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Random;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class SmsServiceImpl implements SmsService {


    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public void sendLoginSms(String phone) throws CustomerException {

        //一键登录 有没有账号都可以发送短信
        //发送登录  没有注册账号就不行
        //发送注册短信 注册了就不行

        //1.生成验证码
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<6;i++){
            sb.append(new Random().nextInt(10));
        }
        //生成的验证码
        String code=sb.toString();

       log.info("code is {}:",code);

        //2.服务器存储验证码
        redisTemplate.boundValueOps(RedisKey.LOGIN_SMS +phone).set(code);

        //3.设置失效时间
        redisTemplate.boundValueOps(RedisKey.LOGIN_SMS +phone).expire(5*60, TimeUnit.SECONDS);

        //4.发送短信给用户
        //SendSms.sendSms(phone,code);

    }
}
